﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    public partial class FolderSyncEditor : Form, IComponentEditor 
    {
        private object _OwnerActivityNode;
        private FolderSyncComponent _FolderSync;

        /// <summary>
        /// ctor
        /// </summary>
        public FolderSyncEditor()
        {
            InitializeComponent();
        }

        private void btn_Browse1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg=new FolderBrowserDialog();
            if(dlg.ShowDialog()==DialogResult.OK)
            {
                this.txt_SrcFolder.Text = dlg.SelectedPath;
            }
        }

        private void btn_Browse2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_TgtFolder.Text = dlg.SelectedPath;
            }
        }

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(FolderSyncComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._OwnerActivityNode = activityNode;
            this._FolderSync = (FolderSyncComponent) wfComponent;
            this.txt_SrcFolder.Text = this._FolderSync.SrcFolderPath;
            this.txt_TgtFolder.Text = this._FolderSync.TargetFolderPath;
            this.txt_Filter.Text = this._FolderSync.ExcludedFileExtensions;
            this.ck_IncludeSubFolder.Checked = this._FolderSync.IncludeSubFolders;
        }

        public void SaveSettings()
        {
            this._FolderSync.SrcFolderPath = this.txt_SrcFolder.Text;
            this._FolderSync.TargetFolderPath = this.txt_TgtFolder.Text;
            this._FolderSync.ExcludedFileExtensions = this.txt_Filter.Text;
            this._FolderSync.IncludeSubFolders = this.ck_IncludeSubFolder.Checked;
        }

        public TestResult Test()
        {
            this.SaveSettings();
            return new TestResult(true,"Succeed");
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent!=null)
            {
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._FolderSync);
            }
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
